<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="姓名" size="small">
            <el-input v-model="searchForm.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addEntry()">添加</el-button>
      <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="550px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="130px">
          <el-form-item label="姓名" size="small" class="item-inline">
            <el-autocomplete
              v-model="teamRow.name"
              :fetch-suggestions="querySearchOperator"
              @select="handleSelectOperator"
              placeholder="请输入内容"
            >
            </el-autocomplete>
          </el-form-item>
          <el-row>
            <el-form-item label="毕业证" size="small" class="item-inline">
            </el-form-item>
            <el-col class="other">
              <el-form-item label="专科(总数)" size="small">
                <el-input type="number" v-model.number="teamRow.specialist"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="other">
              <el-form-item label="本科(总数)" size="small">
                <el-input type="number" v-model.number="teamRow.bachelor"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="职称证" size="small" class="item-inline">
            </el-form-item>
            <el-col class="other">
              <el-form-item label="助理工程师(总数)" size="small">
                <el-input type="number" v-model.number="teamRow.assistantEngineer"></el-input>
              </el-form-item>
              <el-form-item label="高级工程师(总数)" size="small">
                <el-input type="number" v-model.number="teamRow.seniorEngineer"></el-input>
              </el-form-item>
              <el-form-item label="其他(总数)" size="small">
                <el-input type="number" v-model.number="teamRow.other"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="other">
              <el-form-item label="工程师(总数)" size="small">
                <el-input type="number" v-model.number="teamRow.engineer"></el-input>
              </el-form-item>
              <el-form-item label="教授级高工(总数)" size="small">
                <el-input type="number" v-model.number="teamRow.professorEngineer"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col class="other">
              <el-form-item label="注册证(总数)" size="small">
                <el-input type="number" v-model.number="teamRow.registrationCertificate"></el-input>
              </el-form-item>
              <el-form-item label="继续教育证(总数)" size="small">
                <el-input type="number" v-model.number="teamRow.continuingEducationCard"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="other">
              <el-form-item label="职业资格证(总数)" size="small">
                <el-input type="number" v-model.number="teamRow.professionalQualificationCertificate"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="备注说明" size="small">
            <el-input type="textarea" v-model="teamRow.remark" style="min-height: 100px"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
          <el-button size="small" @click="outerVisible = false">取消</el-button>
        </div>
      </el-dialog>
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
    >
      <el-table-column
        slot="opt"
        fixed="right"
        label="操作"
        width="80">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editEntry(scope.row.informationId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    getList,
    addPersonnelInformation,
    selectPersonnelInformation,
    updatePersonnelInformation,
    selectOperator
  } from '@/api/personnelInformation'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const configs = {
    props: ['colConfig'],
    filters: {
      formatMonth(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyyMM')
      },
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyyMMdd')
      },
      formatTime(time) {
        console.log(time)
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      },
      formatter(row, prop) {
        if (row[prop]) {
          let obj = {}
          obj = global[`${prop}List`].find((item) => {
            return item.value === row[prop] // 筛选出匹配数据
          })
          return obj.label
        }
      }
    },
    template: `
  <el-table-column :label="colConfig.label">
            <el-table-column  v-for="(item, index) in colConfig.children"  :label="item.label" :width="item.width">
                <span slot-scope="scope" >
                    <span v-if="item.component==='month'"> {{ scope.row[item.prop] | formatMonth }}</span>
                    <span v-else-if="item.component==='time'"> {{ scope.row[item.prop] | formatDate }}</span>
                    <span v-else-if="item.component==='formatter'"> {{ scope.row,item.prop | formatter }}</span>
                    <span v-else >{{scope.row[item.prop]}}</span>
                </span>
            </el-table-column>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'name', label: '姓名', width: '80', fixed: true },
          {
            label: '毕业证', children: [
              { prop: 'specialist', label: '专科(总数)', width: '80' },
              { prop: 'specialistBorrow', label: '专科(借出数量)', width: '110' },
              { prop: 'bachelor', label: '本科(总数)', width: '80' },
              { prop: 'bachelorBorrow', label: '本科(借出数量)', width: '110' }
            ], component: configs
          },
          {
            label: '职称证', children: [
              { prop: 'assistantEngineer', label: '助理工程师(总数)', width: '120' },
              { prop: 'assistantEngineerBorrow', label: '助理工程师(借出数量)', width: '140' },
              { prop: 'engineer', label: '工程师(总数)', width: '100' },
              { prop: 'engineerBorrow', label: '工程师(借出数量)', width: '120' },
              { prop: 'seniorEngineer', label: '高级工程师(总数)', width: '120' },
              { prop: 'seniorEngineerBorrow', label: '高级工程师(借出数量)', width: '140' },
              { prop: 'professorEngineer', label: '教授级高工(总数)', width: '120' },
              { prop: 'professorEngineerBorrow', label: '教授级高工(借出数量)', width: '140' },
              { prop: 'other', label: '其他(总数)', width: '80' },
              { prop: 'otherBorrow', label: '其他(借出数量)', width: '110' }
            ], component: configs
          },
          { prop: 'registrationCertificate', label: '注册证(总数)', width: '100' },
          { prop: 'registrationCertificateBorrow', label: '注册证(借出数量)', width: '120' },
          { prop: 'professionalQualificationCertificate', label: '职业资格证(总数)', width: '120' },
          { prop: 'professionalQualificationCertificateBorrow', label: '职业资格证(借出数量)', width: '140' },
          { prop: 'continuingEducationCard', label: '继续教育证(总数)', width: '120' },
          { prop: 'continuingEducationCardBorrow', label: '继续教育证(借出数量)', width: '140' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        listLoading: true,
        dialogTitle: '新增人员资料',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        innerVisible: false,
        pca: pca,
        rules: {
          payTime: [
            { required: true, message: '请选择到付日期', trigger: 'change' }
          ],
          receiptPople: [
            { required: true, message: '请输入收件人', trigger: 'change' }
          ],
          paymentMethod: [
            { required: true, message: '请选择寄件类型', trigger: 'change' }
          ]
        },
        visible: false
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      global() {
        return global
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addEntry() {
        this.outerVisible = true
        this.dialogTitle = '新增人员资料'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.dialogType = 1
      },
      editEntry(informationId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改人员资料'
        this.dialogType = 0
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        const params = {
          informationId: informationId
        }
        selectPersonnelInformation(params).then(response => {
          this.teamRow = response.responseBody
        })
      },
      submitForm(formName) {
        const params = {
          employeeId: this.teamRow.employeeId,
          name: this.teamRow.name,
          specialist: this.teamRow.specialist || 0,
          bachelor: this.teamRow.bachelor || 0,
          assistantEngineer: this.teamRow.assistantEngineer || 0,
          engineer: this.teamRow.engineer || 0,
          seniorEngineer: this.teamRow.seniorEngineer || 0,
          professorEngineer: this.teamRow.professorEngineer || 0,
          other: this.teamRow.other || 0,
          registrationCertificate: this.teamRow.registrationCertificate || 0,
          professionalQualificationCertificate: this.teamRow.professionalQualificationCertificate || 0,
          continuingEducationCard: this.teamRow.continuingEducationCard || 0,
          remark: this.teamRow.remark || ''
        }
        if (!this.dialogType) {
          params.informationId = this.teamRow.informationId
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              addPersonnelInformation(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updatePersonnelInformation(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          informationId: scope.row.informationId,
          isDel: 1
        }
        updatePersonnelInformation(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      querySearchOperator(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        if (queryString) {
          const params = {
            name: queryString
          }
          selectOperator(params).then(response => {
            for (const i of response.responseBody) {
              i.value = i.name
            }
            let nameList = response.responseBody
            if (nameList.length === 0) {
              nameList = [{ value: '暂无数据' }]
            }
            callback(nameList)
          })
        } else {
          callback([])
        }
      },
      handleSelectOperator(item) {
        this.teamRow.employeeId = item.employeeId
        this.teamRow.name = item.name
      }
    },
    mounted() {
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  .el-dialog {
    width: 440px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
